package com.example.server.controller;

import com.example.server.entity.Kind;
import com.example.server.entity.Product;
import com.example.server.redis.RedisService;
import com.example.server.service.HistoryService;
import com.example.server.service.ProductService;
import com.example.server.util.Util;
import jakarta.annotation.Priority;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("product")
public class ProductController extends BaseController{

    private ProductService productService;
    @Autowired
    private RedisService redisService;
    public ProductController(ProductService productService) {
        this.productService = productService;
    }
    @PostMapping("create")
    private int create(@RequestPart MultipartFile multipartFile, @RequestPart MultipartFile[] multipartFiles, @RequestPart Product product){
        int sid = Integer.parseInt(redisService.getSetString("storeId"));
        product.setSid(sid);
        String directory = "D:/softwear/springboot-team03/photo/product";
        product.setPicture(Util.savePicture(multipartFile,directory));
        product.setPictures(Util.savePictures(multipartFiles,directory));
        product.setKid(1);
        return productService.create(product);
    }
    @GetMapping("queryAll")
    private List<Product> queryAll(){
        return productService.queryAll();
    }
    @GetMapping("queryById/{id}")
    private Product queryById(@PathVariable int id){
        redisService.setString("productId", String.valueOf(id));
        System.out.println(redisService.getSetString("productId"));
        Product product = productService.queryById(id);
        product.setPictures(product.getPictures().replaceAll("[\\[\\]\" ]", ""));
        return product;
    }
    @GetMapping("queryBySid")
    private List<Product> queryBySid(){
        int sid = Integer.parseInt(redisService.getSetString("storeId"));
        System.out.println("sid: " + sid);
        return productService.queryList("queryBySid", sid);
    }
    @DeleteMapping("removeById/{id}")
    private Product removeById(@PathVariable int id){
        Product product = productService.queryById(id);
        productService.removeById(id);
        return product;
    }
    @PutMapping("modify")
    private int modify(@RequestPart MultipartFile file,@RequestPart Product product){
        int id = Integer.parseInt(redisService.getSetString("productId"));
        product.setId(id);
        String directory = "D:/softwear/springboot-team03/photo/product";
        product.setPicture(Util.savePicture(file,directory));
        return productService.modify(product);
    }
    @PostMapping("deletes")
    private int removeById(@RequestBody int[] ids){
        int i = productService.removeByIds(ids);
        return i;
    }
    @PostMapping("queryByAccount")
    private List<Product> queryByAccount(@RequestBody Product product) {
        int sid = Integer.parseInt(redisService.getSetString("storeId"));
        product.setSid(sid);
        return productService.queryList("queryByAccount", product);
    }
    @GetMapping("queryByFirst")
    private List<Product> queryByFirst(@RequestParam String first){
        return productService.queryList("queryByFirst",first);
    }
    @GetMapping("queryBySecond")
    private List<Product> queryBySecond(@RequestParam Map<String,String> map){
        Kind kind = new Kind();
        kind.setFirst(map.get("first"));
        kind.setSecond(map.get("second"));
        return productService.queryList("queryBySecond",kind);
    }
    @GetMapping("queryByThird")
    private List<Product> queryThird(@RequestParam Map<String,String> map){
        Kind kind = new Kind();
        kind.setFirst(map.get("first"));
        kind.setSecond(map.get("second"));
        kind.setThird(map.get("third"));
        return productService.queryList("queryByThird",kind);
    }

    @GetMapping("queryByTitle")
    private List<Product> queryByTitle(@RequestParam Map<String,String> map){

        String title = map.get("title");
        return productService.queryList("queryByTitle",title);
    }
}
